/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cfs.controller;

import cfs.databean.Fund;
import cfs.databean.FundPriceHistory;
import cfs.formbean.FundHistoryFormBean;
import cfs.model.FundDAO;
import cfs.model.FundPriceHistoryDAO;
import cfs.model.Model;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author danny seow
 */
public class ViewAllFundHistoryAction extends Action{
    private FundPriceHistoryDAO fundPriceDAO; 
    private FundDAO fundDAO;
    
    public ViewAllFundHistoryAction(Model model){
        fundPriceDAO = model.getFundHistoryDao();
        fundDAO = model.getFundDao();
    }
    @Override
    public String getName() {
        return "allFundHistory.do";
    }

    @Override
    public String perform(HttpServletRequest request) {
        ArrayList<String> errors = new ArrayList<String>();
        ArrayList<FundPriceHistory> fundPriceList = new ArrayList<FundPriceHistory>();
        ArrayList<FundHistoryFormBean> fundPriceFormList = new ArrayList<FundHistoryFormBean> ();
        String selectedFund = (String) request.getParameter("selectedFund");
        if(selectedFund != null || selectedFund.length()>0 ){
            try{
                int selectedFundIn = Integer.parseInt(selectedFund);
                fundPriceList = fundPriceDAO.getFundByType(selectedFundIn);
                Fund fund = fundDAO.getFundMap(selectedFundIn);
                if(fundPriceList != null && fundPriceList.size()>0){
                    for(int z=0;z<fundPriceList.size();z++){
                        String price = fundPriceList.get(z).formatDecimal(fundPriceList.get(z).getPrice());
                        String date = fundPriceList.get(z).formatDate(fundPriceList.get(z).getPrice_date());
                        FundHistoryFormBean fb = new FundHistoryFormBean(price,date);
                        fundPriceFormList.add(fb);
                    
                    }
                }
                request.setAttribute("fund", fund);
                request.setAttribute("fundPriceFormList", fundPriceFormList);
                 return "ViewFundHistory.jsp";
            }catch(Exception e){
                errors.add("Not funds was selected");
                request.setAttribute("errors", errors);
                return "ViewFundHistory.jsp";
            }
        }
       errors.add("Not funds was selected");
       request.setAttribute("errors", errors);
       return "ViewFundHistory.jsp";
    }
    
}
